package com.lst.quartz.cluster;

import com.lst.quartz.cluster.job.SimpleRecoveryJob;
import org.quartz.*;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import static org.quartz.DateBuilder.futureDate;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

/**
 * @author shengtao
 * @Description:
 * @date 2018/12/12 11:47
 */
@SpringBootApplication
public class Application implements CommandLineRunner,ApplicationContextAware{

    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }


    @Override
    public void run(String... args) throws Exception {

        Scheduler scheduler = this.applicationContext.getBean(Scheduler.class);
        Class claz = null;
        try {
            claz = Class.forName("com.lst.quartz.cluster.job.SimpleRecoveryJob");
        } catch (ClassNotFoundException e) {
//            LOGGER.error("任务类转化异常，类不存在 class: {}, e:{}",processJob.getZclass(),e.toString());
        }
        JobDetail job = JobBuilder.newJob(claz).withIdentity("myJob1","group1") // put triggers in group
                .requestRecovery() // ask scheduler to re-execute this job if it was in progress when the scheduler went down...
                .build();

//        JobDetail job2 = JobBuilder.newJob(SimpleRecoveryJob.class).withIdentity("job_2" + ":4","group1") // put triggers in group
//                .requestRecovery() // ask scheduler to re-execute this job if it was in progress when the scheduler went down...
//                .build();
//        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("triger_" + "42","group1")
//                .startAt(futureDate(1, DateBuilder.IntervalUnit.SECOND))
//                .withSchedule(simpleSchedule().withRepeatCount(20).withIntervalInSeconds(5)).build();
//
//        SimpleTrigger trigger2 = TriggerBuilder.newTrigger().withIdentity("triger_" + "53","group1")
//                .startAt(futureDate(1, DateBuilder.IntervalUnit.SECOND))
//                .withSchedule(simpleSchedule().withRepeatCount(20).withIntervalInSeconds(5)).build();


        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .forJob("myJob1", "group1")
                .withSchedule(CronScheduleBuilder.cronSchedule("*/1 * * * * ?").withMisfireHandlingInstructionDoNothing())
                .build();
        scheduler.scheduleJob(job, cronTrigger);
//        scheduler.scheduleJob(job2, trigger2);
//        scheduler.start();

    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
